﻿using Cofe.Core.Implements;
using Cofe.Core.Property;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.Core.Property
{
    public static class IPropertyDefinitionExtension
    {
        public static bool IsPropertySupported(this IPropertyDefinitions pd, object property)
        {
            return pd.GetSupportedProperties().Contains(property);
        }

        public static object[] GetSupportedProperties(this IPropertyDefinitions[] pd)
        {
            return pd.SelectMany(ipd => ipd.GetSupportedProperties()).Distinct().ToArray();
        }       

        public static bool IsPropertySupported(this IPropertyDefinitions[] pd, object property)
        {
            return pd.GetSupportedProperties().Contains(property);
        }

        public static IPropertyDefinitions Subset(this IPropertyDefinitions pd, IEnumerable<object> includedProperties)
        {
            return new PropertyDefinitionBase(pd.Category, (p) => includedProperties.Contains(p), pd.BeginId, pd.EndId);
        }

        public static IPropertyDefinitions Subset(this IPropertyDefinitions pd, object[] includedProperties)
        {
            return Subset(pd, includedProperties.ToList());
        }

        public static IPropertyDefinitions[] Merge(IPropertyDefinitions[] propertyDefinitions1, IPropertyDefinitions[] propertyDefinitions2)
        {
            List<IPropertyDefinitions> retVal = new List<IPropertyDefinitions>();
            retVal.AddRange(propertyDefinitions1);
            retVal.AddRange(propertyDefinitions2);
            return retVal.ToArray();
        }
        
        public static IPropertyDefinitions FindDefinition(this IPropertyDefinitions[] pds, int propId)
        {
            foreach (var pd in pds)
                if (propId >= pd.BeginId && propId <= pd.EndId)
                    return pd;
            return null;
        }

    }
}
